<template>
  <div class="app-container">
    <!-- 工具栏 -->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-select
          v-model="query.status"
          clearable
          size="small"
          placeholder="有效标志"
          class="filter-item"
          style="width: 120px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in statusList"
            :key="item.id"
            :label="item.text"
            :value="item.id"
          />
        </el-select>
        <el-input
          v-model="query.supplierName"
          placeholder="请输入供应商"
          size="small"
          class="filter-item"
          style="width: 140px"
        />
        <rrOperation :crud="crud" @resetQuery="onResetQuery"/>
      </div>
      <crudOperation :permissions="permission"></crudOperation>
    </div>
    <!-- 表格渲染 -->
    <el-table
      ref="table"
      v-loading="crud.loading" 
      :data="crud.data"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column v-if="columns.visible('supplierName')" label="供应商" prop="supplierName"></el-table-column>
      <el-table-column v-if="columns.visible('supplierPhone')" label="联系电话" prop="supplierPhone"></el-table-column>
      <el-table-column v-if="columns.visible('supplierWechat')" label="微信" prop="supplierWechat"></el-table-column>
      <el-table-column v-if="columns.visible('supplierAddress')" label="联系地址" prop="supplierAddress" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column v-if="columns.visible('supplierIntroduce')" label="简介" prop="supplierIntroduce" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column v-if="columns.visible('status')" label="有效标志" prop="status">
        <template v-slot="slot">
          <template v-if="slot.row.status === 0">正常</template>
          <template v-if="slot.row.status === 1">无效</template>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <udOperation
            :data="scope.row"
            :permissions="permission"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination />
    <!-- 新增、编辑供应商 -->
    <supplierForm :pharmacyId="pharmacyId"></supplierForm>
  </div>
</template>

<script>
import CRUD, { presenter, header } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import pagination from "@crud/Pagination";
import supplierForm from "./module/form";
import crudSupplier from "@/api/storageRoom/supplier";
import udOperation from '@crud/UD.operation';

let defaultCrud = CRUD({
  title: "供应商",
  url: "api/supplier/getSuppliers",
  crudMethod: { ...crudSupplier },
  query: {supplierName: null, status: null, pharmacyId: null}
});

export default {
  name: "supplier",
  components: { crudOperation, rrOperation, pagination, supplierForm, udOperation },
  mixins: [presenter(defaultCrud), header()],
  data() {
    return {
      permission: [
        {
          type: "add",
          position: 1,
          permission: ["admin", "supplier:add"]
        },
        {
          type: "edit",
          permission: ["admin", "supplier:edit"]
        }
      ],
      statusList: [
        {id: 0, text: "正常"},
        {id: 1, text: "无效"}
      ],
      pharmacyId: null,
      showDialog: false
    }
  },
  created() {
    this.$store.dispatch("GetInfo").then(res => {
      this.query.pharmacyId = res.pharmacyId;
      this.pharmacyId = res.pharmacyId;
      this.crud.toQuery();
    });
  },
  methods: {
    onResetQuery() {
      this.query.pharmacyId = this.pharmacyId;
    }
  }
}
</script>